package com.qsmx.qingqiuticket.app.delegate.group;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.ClickUtil;
import com.qsmx.core.util.CountDownTimerUtil;
import com.qsmx.core.util.StringUtil;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.databinding.DelegateGroupPackageBinding;
import com.qsmx.qingqiuticket.app.delegate.HomeDelegate;
import com.qsmx.qingqiuticket.app.delegate.HomeLoginMemDelegate;
import com.qsmx.qingqiuticket.app.delegate.OrderPayDelegate;
import com.qsmx.qingqiuticket.app.delegate.adapter.GroupPackageTicketListAdapter;
import com.qsmx.qingqiuticket.app.entity.GroupPackageListEntity;
import com.qsmx.qingqiuticket.app.entity.PackageListEntity;
import com.qsmx.qingqiuticket.app.fusion.FusionData;
import com.qsmx.qingqiuticket.app.fusion.FusionTag;

public class GroupPackageDelegate extends AtmosDelegate {
    private CountDownTimerUtil timer;

    private DelegateGroupPackageBinding mBinding;

    private GroupPackageListEntity mGroupPackage;
    private String mVerifyCode = "";

    @Override
    public Object setLayout() {
        return R.layout.delegate_group_package;
    }

    public static GroupPackageDelegate create(GroupPackageListEntity groupPackageList, String verifyCode) {

        final Bundle args = new Bundle();
        args.putSerializable(FusionTag.GROUP_PACKAGE, groupPackageList);
        args.putString(FusionTag.VERIFY_CODE, verifyCode);

        final GroupPackageDelegate delegate = new GroupPackageDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mGroupPackage = (GroupPackageListEntity) args.getSerializable(FusionTag.GROUP_PACKAGE);
            mVerifyCode = args.getString(FusionTag.VERIFY_CODE);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mBinding = DelegateGroupPackageBinding.bind(rootView);

        mBinding.tvPackageName.setText(mGroupPackage.getBody().getPackageInfo().getPackageName());
        mBinding.tvPackageDesc.setText(mGroupPackage.getBody().getPackageInfo().getSubHeading());
        mBinding.tvPackageNum.setText("x" + mGroupPackage.getBody().getPackageNum());
        mBinding.tvSelectNum.setText(String.valueOf(mGroupPackage.getBody().getPackageNum()));

        GroupPackageTicketListAdapter mAdapter = new GroupPackageTicketListAdapter(getContext());
        mAdapter.setData(mGroupPackage.getBody().getItemList());
        mBinding.rlvPackageTicketList.setAdapter(mAdapter);
        mBinding.rlvPackageTicketList.setLayoutManager(new LinearLayoutManager(getContext()));

        mBinding.tvSelectNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtil.IntegerValueOf(mBinding.tvSelectNum.getText().toString(), 0) <= 1) {
                    mBinding.ivBuyMin.setEnabled(false);
                    mBinding.ivBuyMin.setImageResource(R.mipmap.icon_gary_min);
                } else {
                    mBinding.ivBuyMin.setEnabled(true);
                    mBinding.ivBuyMin.setImageResource(R.mipmap.icon_orange_min);
                }
            }
        });

        mBinding.ivBuyMin.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                if (StringUtil.IntegerValueOf(mBinding.tvSelectNum.getText().toString(), 0) > 1) {
                    mBinding.tvSelectNum.setText(StringUtil.IntegerValueOf(mBinding.tvSelectNum.getText().toString(), 0) - 1 + "");
                }
            }
        });

        mBinding.ivBuyAdd.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                if (StringUtil.IntegerValueOf(mBinding.tvSelectNum.getText().toString(), 0) < mGroupPackage.getBody().getPackageNum()) {
                    mBinding.tvSelectNum.setText(StringUtil.IntegerValueOf(mBinding.tvSelectNum.getText().toString(), 0) + 1 + "");
                }
            }
        });

        mBinding.tvBackHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isNotFastClick()) {
                    getSupportDelegate().popTo(HomeDelegate.class, false);
                }
            }
        });

        mBinding.clLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isNotFastClick()) {
                    getSupportDelegate().start(new HomeLoginMemDelegate());
                }
            }
        });

        mBinding.tvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isNotFastClick()) {
                    int doorTicketNum = 0;
                    int onceTicketNum = 0;
                    for (GroupPackageListEntity.BodyBean.ItemListBean data : mGroupPackage.getBody().getItemList()) {
                        if (data.getType().equals("1")) {
                            doorTicketNum++;
                        } else if (data.getType().equals("3")) {
                            onceTicketNum++;
                        }
                    }
                    getSupportDelegate().start(GroupSessionListDelegate.create(mGroupPackage.getBody().getOrderNo(), mGroupPackage.getBody().getPackageId(),
                            mGroupPackage.getBody().getPackageInfo().getPackageName(),
                            mBinding.tvSelectNum.getText().toString(),
                            doorTicketNum, onceTicketNum,
                            mVerifyCode, mGroupPackage.getBody().getPackageInfo().isShowSessionDesc(),
                            mGroupPackage.getBody().getPerGiveIntegral(),
                            mGroupPackage.getBody().getPerGiveGrowth()
                    ));
                }
            }
        });
    }

    private void startCountDownTime(final long time) {
        if (timer != null) {
            timer.cancel();
        }
        timer = new CountDownTimerUtil(time * 1000, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                mBinding.tvBackHome.setText(String.format(getString(R.string.back_home), String.valueOf(millisUntilFinished / 1000)));
            }

            @Override
            public void onFinish() {
                getSupportDelegate().popTo(HomeDelegate.class, false);
            }
        };
        timer.start();// 开始计时
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        startCountDownTime(60);
        if (FusionData.mMemData != null) {
            mBinding.tvMemInfo.setText(StringUtil.getPhoneNum(FusionData.mMemData.getPhone()));
        }
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
        }
        super.onDestroy();
        FusionData.mMemData = null;
    }
}
